﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using Results.Models;

namespace Results.ViewModels
{
    /// <summary>
    /// A golf tournament
    /// </summary>
    public class TournamentViewModel : ViewModelBase
    {
        /// <summary>
        /// The scores for this tournament
        /// </summary>
        public ObservableCollection<ResultViewModel> Results { get; private set; }

        /// <summary>
        /// Command to invoke when this tournament is selected
        /// </summary>
        public RelayCommand TournamentSelected { get; private set; }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="tournamentModel">Source data</param>
        /// <param name="notifySelection">Methods to call if the user selects something</param>
        public TournamentViewModel(TournamentModel tournamentModel, INotifySelection notifySelection)
        {
            _tournamentModel = tournamentModel;

            TournamentSelected = new RelayCommand(p => notifySelection.TournamentSelected(tournamentModel));

            Name = tournamentModel.Name;
            Course = tournamentModel.Course;
            Location = tournamentModel.Location;
            Website = tournamentModel.Website;
            Season = tournamentModel.Season;
            Weather = tournamentModel.Weather;
       
            if (tournamentModel.From.Month == tournamentModel.To.Month)
            {
                FormattedDates = tournamentModel.From.ToString("MMM dd - ") + tournamentModel.To.ToString("dd, yyyy");
            }
            else
            {
                FormattedDates = tournamentModel.From.ToString("MMM dd -") + tournamentModel.To.ToString("MMM dd, yyyy");
            }

            Results = new ObservableCollection<ResultViewModel>();
        }

        /// <summary>
        /// Find all tournaments that contain results for the selected group
        /// </summary>
        /// <param name="selectedGroup">e.g. "Men 14-16"</param>
        /// <param name="notifyPlayerSelected">Methods to call if the user selects something</param>
        public void Query(string selectedGroup, INotifySelection notifyPlayerSelected)
        {
            Results.Clear();

            foreach (var resultModel in
                from resultModel in _tournamentModel.Results
                where resultModel.Group == selectedGroup
                orderby resultModel.PointValue descending
                select new { ResultModel = resultModel })
            {
                Results.Add(new ResultViewModel(resultModel.ResultModel, notifyPlayerSelected));
            }
        }

        String _name;
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    Notify("Name");
                }
            }
        }

        String _course;
        public String Course
        {
            get
            {
                return _course;
            }
            set
            {
                if (_course != value)
                {
                    _course = value;
                    Notify("Course");
                }
            }
        }

        String _location;
        public String Location
        {
            get
            {
                return _location;
            }
            set
            {
                if (_location != value)
                {
                    _location = value;
                    Notify("Location");
                }
            }
        }

        String _formattedDates;
        public String FormattedDates
        {
            get
            {
                return _formattedDates;
            }
            set
            {
                if (_formattedDates != value)
                {
                    _formattedDates = value;
                    Notify("FormattedDates");
                }
            }
        }

        String _website;
        public String Website
        {
            get
            {
                return _website;
            }
            set
            {
                if (_website != value)
                {
                    _website = value;
                    Notify("Website");
                }
            }
        }

        String _season;
        public String Season
        {
            get
            {
                return _season;
            }
            set
            {
                if (_season != value)
                {
                    _season = value;
                    Notify("Season");
                }
            }
        }

        String _weather;
        public String Weather
        {
            get
            {
                return _weather;
            }
            set
            {
                if (_weather != value)
                {
                    _weather = value;
                    Notify("Weather");
                }
            }
        }

        readonly TournamentModel _tournamentModel;
    }
}
